package websocketgulu.demo.socket;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

//封装socket对象 ClientSocket
// 每个链接都是一个ClientSocket对象.我们可以在此类中和客户端进行收到等操作达到监听效果.
// 也可以将收到的数据保存到数据库中,以便后续业务需求.
@Slf4j
@Data
public class ClientSocket implements Runnable {


    private Socket socket;
    private DataInputStream inputStream;
    private DataOutputStream outputStream;
    private String key;
    private String message;

    @Override
    public void run() {
        while (true){
            try {
                SocketHandler.onMessage(this);

            } catch (Exception e) {
                e.printStackTrace();
            }
            if (SocketHandler.isSocketClosed(this)){
                System.out.println("连接关闭");
                SocketHandler.close(this);
                break;
            }
        }
    }
}